<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>max 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-metrics-max-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-max-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-max-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-metrics-max-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-metrics-max-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-metrics.html">度量聚合</a></span>
»
<span class="breadcrumb-node">max 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-metrics-geocentroid-aggregation.html">« geo_centroid 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-min-aggregation.html">min 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-metrics-max-aggregation"></a>max 聚合
</h2>
</div></div></div>
<p>
<code class="literal">single-value</code>(单值) 度量聚合，跟踪并返回从聚合文档中提取的数值的最大值。

这些值可以从文档中指定的 numeric 字段中提取，也可以由提供的脚本生成。
</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>
<code class="literal">min</code> 和 <code class="literal">max</code> 聚合对数据以 <code class="literal">double</code>(双精度) 进行操作。

因此，在绝对值大于 <code class="literal">2^53</code> 的长整型上运行时，结果可能是近似的。
</p>
</div>
</div>
<p>计算所有文档的最高价格(max_price)：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "aggs" : {
        "max_price" : { "max" : { "field" : "price" } }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/353.console"></div>
<p>响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "max_price": {
            "value": 200.0
        }
    }
}</pre>
</div>
<p>
可以看到，聚合的名称(上面的<code class="literal">max_price</code>)也作为键，通过它可以从返回的响应中检索聚合结果。
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_script_7"></a>脚本(script)
</h3>
</div></div></div>
<p>
<code class="literal">max</code> 聚合还可以计算脚本的最大值。以下示例计算最高价格：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search
{
    "aggs" : {
        "max_price" : {
            "max" : {
                "script" : {
                    "source" : "doc.price.value"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/354.console"></div>
<p>
这将使用 <a class="xref" href="modules-scripting-painless.html" title="Painless scripting language" rel="nofollow">painless</a>(无痛) 脚本语言，没有脚本参数。

要使用存储的脚本，请使用以下语法：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search
{
    "aggs" : {
        "max_price" : {
            "max" : {
                "script" : {
                    "id": "my_script",
                    "params": {
                        "field": "price"
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/355.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_value_script_4"></a>值脚本 (Value Script)
</h3>
</div></div></div>
<p>
假设索引中的文档价格以美元为单位，但是我们想以欧元计算最大值(这个例子中，假设兑换率是1.2)。

我们可以使用值脚本，在汇总每个值之前对其应用转换率(conversion_rate)：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search
{
    "aggs" : {
        "max_price_in_euros" : {
            "max" : {
                "field" : "price",
                "script" : {
                    "source" : "_value * params.conversion_rate",
                    "params" : {
                        "conversion_rate" : 1.2
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/356.console"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_missing_value_6"></a>缺失的值
</h3>
</div></div></div>
<p>
参数 <code class="literal">missing</code> 定义应该如何处理有缺失值的文档。

默认情况下，它们将被忽略，但也可以将它们视为有一个值。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search
{
    "aggs" : {
        "grade_max" : {
            "max" : {
                "field" : "grade",
                "missing": 10 <a id="CO191-1"></a><i class="conum" data-value="1"></i>
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/357.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO191-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>
<code class="literal">grade</code> 字段没有值的文档将与值为<code class="literal">10</code>的文档落入同一个桶中。
</p>
</td>
</tr>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-metrics-geocentroid-aggregation.html">« geo_centroid 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-min-aggregation.html">min 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>